package com.luokun.vue_shop.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.luokun.vue_shop.api.Json;
import com.luokun.vue_shop.api.Result;
import com.luokun.vue_shop.entity.SpManager;
import com.luokun.vue_shop.mapper.SpManagerMapper;
import com.luokun.vue_shop.model.SpManagerModel;
import com.luokun.vue_shop.model.SpPermissionApiModel;
import com.luokun.vue_shop.service.ISpManagerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

@CrossOrigin(origins = "*")
@RestController
@Slf4j
@RequestMapping("/SpManager")
public class SpManagerController {

    @Autowired
    private ISpManagerService iSpManagerService;

    @Resource
    private SpManagerMapper spManagerMapper;

    /**
     * 查询管理员数据
     *
     * @param pagenum
     * @param pageSize
     * @param response
     * @param query
     * @throws Exception
     */
    @GetMapping("/users")
    public void users(@RequestParam(name = "pagenum", defaultValue = "1") Integer pagenum,
                      @RequestParam(name = "pageSize", defaultValue = "2") Integer pageSize,
                      @RequestParam(name = "query", defaultValue = "") String query,
                      HttpServletResponse response) throws Exception {
        ///查询结果
        IPage<SpManagerModel> iPage = iSpManagerService.getSpManager(pagenum, pageSize, query);
        Result result = new Result(true, 200, "获取管理员列表成功", iPage.getRecords(), iSpManagerService.getcount(query));
        Json.toJson(result, response);
    }


    /**
     * 状态信息修改
     *
     * @param mgId
     * @param mgState
     * @param response
     * @throws Exception
     */
    @PutMapping("/users")
    public void upd(@RequestParam("mgId") Integer mgId, @RequestParam("mgState") Integer mgState,
                    HttpServletResponse response) throws Exception {
        int count = iSpManagerService.updSpManager(mgId, mgState == 0 ? 1 : 0);
        if (count > 0) {
            Result result = new Result(true, 200, "状态修改成功");
            Json.toJson(result, response);
        } else {
            Result result = new Result(true, 7000, "状态修改失败");
            Json.toJson(result, response);
        }
    }

    @PostMapping("/add")
    public void insert(@RequestBody SpManager spManager, HttpServletResponse response) throws Exception {
        System.out.println(spManager);
        try {
            //默认超级管理员
            spManager.setRoleId(41);
            spManager.setMgTime(new Date());
            iSpManagerService.save(spManager);
            Result result = new Result(true, 200, "新增成功");
            Json.toJson(result, response);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            Result result = new Result(true, 7000, "新增失败");
            Json.toJson(result, response);
        }

    }


    @GetMapping("/edit")
    public void edit(@RequestParam("id") Integer id, HttpServletResponse response) throws Exception {
//        QueryWrapper<SpManager> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("mg_id", id);
        SpManager spManager = spManagerMapper.selectById(id);
        if (spManager == null) {
            Result result = new Result(true, 7000, "编辑数据加载失败");
            Json.toJson(result, response);
        }
        Result result = new Result(true, 200, "编辑数据加载成功", spManager);
        Json.toJson(result, response);
    }

    @PostMapping("/upd")
    public void upd(@RequestBody SpManager spManager, HttpServletResponse response) throws Exception {
        Boolean bool = iSpManagerService.updateById(spManager);
        if (bool) {
            Result result = new Result(true, 200, "修改成功");
            Json.toJson(result, response);
        } else {
            Result result = new Result(true, 7000, "修改失败");
            Json.toJson(result, response);
        }
    }

    @DeleteMapping("/remove")
    public void remove(@RequestParam("id") Integer id, HttpServletResponse response) throws Exception {
        Boolean bool = iSpManagerService.removeById(id);
        if (bool) {
            Result result = new Result(true, 200, "删除成功");
            Json.toJson(result, response);
        } else {
            Result result = new Result(true, 7000, "删除失败");
            Json.toJson(result, response);
        }

    }


    @GetMapping("/rights")
    public void rights(HttpServletResponse response) throws Exception {
       List<SpPermissionApiModel> list= iSpManagerService.getRightsList();
       if (list!=null){
           Result result = new Result(true, 200, "数据加载成功",list);
           Json.toJson(result, response);
       }else {
           Result result = new Result(true, 7000, "数据加载失败");
           Json.toJson(result, response);
       }
    }



}
